package com.hnkywl.sts.action.organ;

import com.hnkywl.sts.entity.organ.Khh;
import com.hnkywl.sts.entity.organ.Qyfr;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.service.base.WebserviceLogService;
import com.hnkywl.sts.webservice.bg.client.SupplierStub;
import com.hnkywl.sts.webservice.bg.response.Provide;
import com.hnkywl.sts.webservice.bg.util.EncryptUtil;
import com.hnkywl.sts.webservice.bg.util.StackTraceUtil;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.client.ServiceClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 根据供应商编码，查询保供供应商详细信息，通过接口
 * Created with IntelliJ IDEA.
 * User: nianjunyan
 * Date: 13-6-6
 * Time: 下午4:18
 * To change this template use File | Settings | File Templates.
 */
@Component
public class GetBgProvide {
    private SupplierStub supplierStub;
    private WebserviceLogService webserviceLogService;

    /**
     * @param provideCode
     * @return
     */
    public Provide getBgProvideDetail(String provideCode) {
        Provide provide = null;
        String sightCon = null;
        String exception = "";
        SupplierStub supplier = null;
        try {
//            SupplierStub.GetSupplierByCode sh = new SupplierStub.GetSupplierByCode();
//            sh.setCode(provideCode);
//            sh.setSystemSecurityCode("CB0B2563E78014816403EBDF7C63D4D3");
//            ServiceClient client = supplierStub._getServiceClient();
//            sightCon = "CB0B2563E78014816403EBDF7C63D4D3" + provideCode;
//            generateESBSecurityHeader(sightCon, client);
//            supplier = supplierStub.getSupplierByCode(sh).get_return()[0];
//            provide = dateDeal(supplier);
        } catch (Exception e) {
            e.printStackTrace();
            exception = StackTraceUtil.getStackTrace(e);
        }
        webserviceLogService.saveClientLog(
                GetBgProvide.class.getName(),
                "getBgProvideDetail",
                "sight:" + sightCon + "provideCode:" + provideCode,
                supplier != null ? "企业全称:" + 0 : "【未返回数据】",
                exception,
                ""
        );
        return provide;
    }

    private void generateESBSecurityHeader(String sightCon, ServiceClient client) {
        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace SecurityElementNamespace = factory.createOMNamespace(
                "", "");

        OMElement usernameEl = factory.createOMElement("username",
                SecurityElementNamespace);
        usernameEl.setText("hnkywl");
        OMElement passwordEl = factory.createOMElement("password",
                SecurityElementNamespace);
        passwordEl.setText(EncryptUtil.MD5("hnkywl"));
        OMElement signTextEl = factory.createOMElement("signText",
                SecurityElementNamespace);
        String sighText = "C4D496A4ED899FB5DEB9E7685186EE15" + sightCon;
        signTextEl.setText(EncryptUtil.MD5(sighText));
        client.addHeader(usernameEl);
        client.addHeader(passwordEl);
        client.addHeader(signTextEl);

    }

    /**
     * 数据处理
     *
     * @param supplier
     * @return
     */
    private static Provide dateDeal(SupplierStub supplier) {
        Provide provide = new Provide();
//        /**********************企业机构基本信息*************************************************************/
//        Qyjg qyjg = new Qyjg();
//        qyjg.setQc(supplier.getName());
//        if (StringUtils.isNotEmpty(supplier.getEnterpriseShortName()))
//            qyjg.setJc(supplier.getEnterpriseShortName());
//        if (StringUtils.isNotEmpty(supplier.getEnterpriseShortName()))
//            qyjg.setJc(supplier.getEnterpriseShortName());
//        if (StringUtils.isNotEmpty(supplier.getContactPerson()))
//            qyjg.setLxr(supplier.getContactPerson());
//        if (StringUtils.isNotEmpty(supplier.getTel()))
//            qyjg.setLxdh(supplier.getTel());
//        qyjg.setLastUpdated(supplier.getLastUpdateDate());
//        provide.setQyjg(qyjg);
//        /**********************企业法人信息*************************************************************/
//        Qyfr qyfr = new Qyfr();
//        if (StringUtils.isNotEmpty(supplier.getEnterpriseFullName()))
//            qyfr.setQyqc(supplier.getEnterpriseFullName());
//        if (StringUtils.isNotEmpty(supplier.getEnterpriseBankShortName()))
//            qyfr.setQyjc(supplier.getEnterpriseFullName());
//        if (StringUtils.isNotEmpty(supplier.getCorporationBankName()))
//            qyfr.setKhh(supplier.getCorporationBankName());
//        if (StringUtils.isNotEmpty(supplier.getCorporationBankAccount()))
//            qyfr.setZh(supplier.getCorporationBankAccount());
//        if (StringUtils.isNotEmpty(supplier.getCorporationBankAccount()))
//            qyfr.setZh(supplier.getCorporationBankAccount());
//        if (StringUtils.isNotEmpty(supplier.getCorporationAgent()))
//            qyfr.setFddlr(supplier.getCorporationAgent());
//        if (StringUtils.isNotEmpty(supplier.getCorporationTaxNumber()))
//            qyfr.setNsh(supplier.getCorporationTaxNumber());
//        if (StringUtils.isNotEmpty(supplier.getCorporationDelegateAgent()))
//            qyfr.setWtdlr(supplier.getCorporationDelegateAgent());
//        if (StringUtils.isNotEmpty(supplier.getCorporationTel()))
//            qyfr.setDh(supplier.getCorporationTel());
//        if (StringUtils.isNotEmpty(supplier.getCorporationOfficeAddress()))
//            qyfr.setBgdz(supplier.getCorporationOfficeAddress());
//        if (StringUtils.isNotEmpty(supplier.getCorporationFaxNumber()))
//            qyfr.setCz(supplier.getCorporationFaxNumber());
//        if (StringUtils.isNotEmpty(supplier.getCorporationZip()))
//            qyfr.setYzbm(supplier.getCorporationZip());
//        if (StringUtils.isNotEmpty(supplier.getEnterpriseTaxBankName()))
//            qyfr.setNskhh(supplier.getEnterpriseTaxBankName());
//        qyfr.setLastUpdated(supplier.getLastUpdateDate());
//        provide.setQyfr(qyfr);
//        /**********************企业开户行*************************************************************/
//        Khh khh = new Khh();
//        if (StringUtils.isNotEmpty(supplier.getEnterpriseBankShortName()))
//            khh.setJc(supplier.getEnterpriseBankShortName());
//        if (StringUtils.isNotEmpty(supplier.getEnterpriseBankFullName()))
//            khh.setQc(supplier.getEnterpriseBankFullName());
//        if (StringUtils.isNotEmpty(supplier.getEnterpriseBankAccount()))
//            khh.setZh(supplier.getEnterpriseBankAccount());
//        khh.setLastUpdated(supplier.getLastUpdateDate());
//        provide.setKhh(khh);
        return provide;
    }

    @Autowired
    public void setWebserviceLogService(WebserviceLogService webserviceLogService) {
        this.webserviceLogService = webserviceLogService;
    }

    @Autowired
    public void setSupplierStub(SupplierStub supplierStub) {
        this.supplierStub = supplierStub;
    }
}
